﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using Jb.Web.Utility;
using AutoMapper;
using Jb.Web.ViewModels.UtilityModels;

namespace Jb.Web.Areas.Admin.Controllers
{
    [Authorize(Roles="Admin")]
    public class PackageController : BaseController
    {
		private JbEntities _Context = new JbEntities();
        
		#region Constructors
		public PackageController()
		{
		}

		public PackageController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return View("List", BuildPackageListConfig());
        }

		public ActionResult List()
        {
            return View(BuildPackageListConfig());
        }

		/// <summary>
        /// Gets a Package from Database to edit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
		public ActionResult Edit(int id = 0, String IsSaved = null)
        {
            Package entity = id == 0 ? new Package() : _Context.Packages.FirstOrDefault(P => P.ID == id);            
            PackageViewModel viewModel = Mapper.Map<Package, PackageViewModel>(entity);             
            if (String.Compare(IsSaved, "true", true) == 0)
                ViewData[AppConstants.ViewDataKeys.SAVED] = true;
			return View(viewModel);
        }

		/// <summary>
        /// Saves Package Information into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
		[HttpPost]
		public ActionResult Edit(PackageViewModel viewModel)
        {
			if (!ModelState.IsValid)
                return View(viewModel);

			Package entity = null; 
			if (viewModel.ID == 0)
            {
                entity = new Package();
                entity.Created = DateTime.Now;
                _Context.AddToPackages(entity);
            }
            else
                entity = _Context.Packages.FirstOrDefault(P => P.ID == viewModel.ID);
            
            entity = Mapper.Map<PackageViewModel, Package>(viewModel, entity);
            entity.Modified = DateTime.Now;
		
            _Context.SaveChanges();
            //return RedirectToAction("Index");
			return RedirectToAction(String.Format("Edit/{0}", entity.ID), new { IsSaved = "true" });
        }		
		#endregion Action Methods
		
		#region Helper Methods

		/// <summary>
        /// Builds a ListConfig object with a list of Package
        /// </summary>
        /// <returns></returns>
		private ListConfig BuildPackageListConfig()
		{
			UtilityDAO dao = new UtilityDAO();
            Jb.Domain.DbParameter[] parameters = new[]{
                new Jb.Domain.DbParameter("@GetActiveOnly", false)
            };
            DataSet ds = dao.GetDataSet(DbObject.SP.GET_PACKAGE_LIST, parameters, true);
            ListConfig listConfig = new ListConfig(ds.Tables[0], null);
            return listConfig;
		}

        #endregion			
    }
}


